package com.controller;

import com.domain.LoginUser;
import com.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 负责修改密码
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/passwordRest")
    public String passwordRest(String oldPassword, String password1, HttpServletRequest request){
        //获取当前登录用户信息
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        if (!user.getPassword().equals(oldPassword)){
            request.setAttribute("message","密码输入错误！");
            return "error";
        }
        user.setPassword(password1);
        request.setAttribute("user",user);
        loginService.updatePassword(user);
        request.setAttribute("message","返回登录");
        return "success";
    }

    @RequestMapping("/toPassword")
    public String toPassword(HttpServletRequest request){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        String redirectTo = null;
        if (user.getRole() == 2){
            redirectTo = "student";
        }else if (user.getRole() == 1){
            redirectTo = "teacher";
        }else {
            redirectTo = "admin";
        }
        return redirectTo+"/passwordRest";
    }
}
